﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;

public partial class HandleTransaction : System.Web.UI.Page
{
    private string _orderID;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["OrderID"] != null)
        {
            _orderID = Request.QueryString["OrderID"];

            BindCompanyInfo();
            BindDealInfo();
        }

        if (!IsPostBack)
        {
            BindBanks();
        }
    }

    /// <summary>
    /// 新增入账记录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(_orderID))
        {
            var errormsg = string.Empty;

            var paytype = Convert.ToInt32(DropDownList1.SelectedValue);
            var paddate = Convert.ToDateTime(PayDate.Text.Trim());
            var amount = Convert.ToInt32(Amount.Text.Trim());
            // 如果是少缴款，转为负数
            amount = paytype == 5 ? -amount : amount;
            var bank = Convert.ToInt32(DropDownList2.SelectedValue);
            var remark = Remark.Text.Trim();
            var empolyeeID = new EmpolyeeBLL().GetEmpolyeeInfoByLoginName(User.Identity.Name).EmpolyeeID;

            new PaymentBLL().AddPaymentRecord(_orderID, paytype, paddate, amount, bank, remark, empolyeeID, out errormsg);

            if (!string.IsNullOrEmpty(errormsg))
            {
                ErrorMessage.Text = errormsg;
                return;
            }

            GridView1.DataBind();
        }
    }

    /// <summary>
    /// 绑定公司信息
    /// </summary>
    private void BindCompanyInfo()
    {
        if (!string.IsNullOrEmpty(_orderID))
        {
            var compaySerialNumber = new OrderBLL().GetCompanySerialNumberByOrderID(_orderID);

            var company = new CompanyBLL().GetCompanyBySerialNumber(compaySerialNumber);

            if (company != null)
            {
                if (company.CompanyID != null)
                {
                    CompanyID.Text = company.CompanyID;
                }

                RegisterRegion.Text = new RegionBLL().GetRegionNameByRegionID(Convert.ToInt32(company.RegionID));
                EnglishName.Text = company.EnglishName;
                ChineseName.Text = company.ChineseName;
            }
        }
    }

    /// <summary>
    /// 绑定银行
    /// </summary>
    private void BindBanks()
    {
        DataTable dt = new BankBLL().GetBanks();

        DropDownList2.DataSource = dt;
        DropDownList2.DataTextField = "BankName";
        DropDownList2.DataValueField = "BankID";
        DropDownList2.DataBind();
    }

    private void BindDealInfo()
    {
        if (!string.IsNullOrEmpty(_orderID))
        {
            var order = new OrderBLL().GetOrderInfoByOrderID(_orderID);

            if (order != null)
            {
                OrderTime.Text = order.OrderTime.ToString("yyyy-MM-dd");
                DelegateType.Text = new DelegateBLL().GetDelegateNameByDelegateType(order.DelegateTypeID);
                Price.Text = order.Amount.ToString();

                CurrencyType.Text = RemarkAttribute.GetEnumRemark(order.CurrencyType);
                Branch.Text = new BranchBLL().GetBranchNameByBranchID(order.BranchID);
                Empolyee.Text = new EmpolyeeBLL().GetEmpolyeeNameByEmpolyeeID(order.EmpolyeeID);
                OrderRemark.Text = order.Remark;
            }
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(_orderID))
        {// 写入订单轨迹
            var empolyeeID = new EmpolyeeBLL().GetEmpolyeeInfoByLoginName(User.Identity.Name).EmpolyeeID;
            new OrderTrackBLL().AddTrack(_orderID, empolyeeID, Koonsoft.HYCRM.Model.OrderStatus.OrderTransaction, 1, "业务员报单后分行财务入账(订单首次付款)");

            // 通知订单状态改变
            new OrderBLL().ChangeOrderStatusByOrderID(_orderID, Koonsoft.HYCRM.Model.OrderStatus.OrderTransaction);

            Response.Redirect("OrdersForTransaction.aspx");
        }
    }
}